package com.myapp.ruyixing.http;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.myapp.ruyixing.model.WholeCity;

public class WholeCityParser extends RYJSONParser {

	@Override
	public Object parse(JSONObject jsonObject) {
		if (getErrno(jsonObject) != NO_ERROR) {
			return null;
		}
		
		WholeCity wholeCity = new WholeCity();
		
		JSONObject data = jsonObject.optJSONObject("data");
		JSONArray districts = data.optJSONArray("districts");
		JSONArray categories = data.optJSONArray("categories");
		
		if (districts != null) {
			wholeCity.districts = new ArrayList<WholeCity.WholeCityCell>();
			for (int i = 0; i < districts.length(); i++) {
				JSONObject cell = districts.optJSONObject(i);
				
				if (cell != null) {
					WholeCity.WholeCityCell cellModel = new WholeCity.WholeCityCell();
					cellModel.id = cell.optString("id");
					cellModel.name = cell.optString("name");
					
					wholeCity.districts.add(cellModel);
				}
			}
		}
		
		if (categories != null) {
			wholeCity.categories = new ArrayList<WholeCity.WholeCityCell>();
			for (int i = 0; i < categories.length(); i++) {
				JSONObject cell = categories.optJSONObject(i);
				
				if (cell != null) {
					WholeCity.WholeCityCell cellModel = new WholeCity.WholeCityCell();
					cellModel.id = cell.optString("id");
					cellModel.name = cell.optString("name");
					
					wholeCity.categories.add(cellModel);
				}
			}
		}
		
		return wholeCity;
	}
}
